<?php
namespace app\controller;
use app\model\Profile;
use app\model\User as UserModel;

class Grade
{
    public function index(){
        //$user = UserModel::find(19);
        //return json($user->profile);
        //echo $user->profile->hobby;
        //查询到后进行修改
        //$user->profile->save(['hobby'=>'喜欢大姐姐']);
        //关联新增
        //$user->profile()->save(['hobby'=>'酷爱小姐姐']);

        return json(UserModel::has('profile','>=',2)->select());

    }

    public function load(){
//        $list = UserModel::select([19,20,21]);
//        foreach($list as $user){
//            dump($user->profile->toArray());
//        }
        $list = UserModel::with(['profile'])->select([19,20,21]);
        foreach($list as $user){
            dump($user->profile->toArray());
        }
    }

    public function count(){
        $list = UserModel::withCount('profile')->select([19,20,21]);
        foreach ($list as $user){
            echo $user->profile_count;
            echo '<br>';
        }
    }

    public function many(){
        $user = UserModel::find(19);
        $roles = $user->roles;
        return json($roles);
    }
}